<?php

namespace App\Service\UserOperation;

use App\Constants\ErrorCode;
use App\Exception\ApiException;
use App\Model\Gift;
use App\Model\GiftLog;
use App\Model\PlatformLimit;
use Hyperf\DbConnection\Db;

class SendGiftCheckService
{
    /**
     * 充值的礼物
     * @var
     */
    public $giftId;

    /**
     * 充值礼物的数量
     * @var
     */
    public $giftNum;

    /**
     * 执行用户id
     * @var
     */
    public $actionUserId;

    public function check()
    {
        //获取限制 如果查不到限制数据就是没有限制
        $limit = PlatformLimit::where([
            ['type', '=', PlatformLimit::SEND_GIFT_POPOVER],
            ['status', '=', 1]
        ])->orderBy('id', 'desc')->value('value');
        if (empty($limit)) {
            return true;
        }

        $totalCoin = GiftLog::checkTotalCost($this->actionUserId, date('Y-m-d'));

        $giftCoin = Gift::getInfo($this->giftId)['coin'] * $this->giftNum;
        if (empty($giftCoin)) {
            throw new ApiException('系统错误');
        }

        if ($giftCoin + $totalCoin >= $limit) {
            //您今日送礼金额已达上限，可与对方沟通交流感情，切勿冲动消费。
            throw new ApiException('', ErrorCode::SEND_GIFT_POPOVER);
        }

        return true;
    }
}